import os
import sys
import datetime
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains


class LikeShopTest():
    def __init__(self) -> None:
        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)
    
    def login_test(self):
        self.browser.get("http://likeshop.abc:8080/admin/")
        sleep(1)
        self.browser.find_element(By.NAME, 'account').send_keys('admin')
        self.browser.find_element(By.NAME, 'password').send_keys('123456')
        self.browser.find_element(By.ID, 'login').click()
        sleep(2)
        
    def shop_list_test(self):
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[2]/div/ul/li[1]/a').click()
        sleep(1)
        iframe1 = self.browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        self.browser.find_element(By.ID, 'keyword').send_keys('文具')
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/div[3]/button[1]').click()
        sleep(2)
        tr_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        is_not_bug = True
        if len(tr_list) == 0:
            pass
        else:
            for tr in tr_list:
                td = tr.find_element(By.XPATH, './td[2]/div')
                title = td.text
                if '文具' not in title:
                    is_not_bug = False
        if is_not_bug:
            pass
        self.browser.switch_to.default_content()
        sleep(1)
        
        
        
            
    def shop_add_test(self):
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[2]/div/ul/li[1]/a').click()
        sleep(1)
        iframe1 = self.browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        sleep(2)
        iframe_add = self.browser.find_element(By.XPATH, '//*[@id="layui-layer-iframe1"]')
        self.browser.switch_to.frame(iframe_add)
        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        self.browser.find_element(By.NAME, 'name').send_keys("测试商品" + now)
        sleep(1)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(1)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(1)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
        sleep(1)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        sleep(1)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(2)
        self.upload_test('//*[@id="layui-layer-iframe2"]', 'D:\\HuaweiMoveData\\Users\\weidajin\\Documents\\project\\image\\F62F47789AC1A7ED4F938612ED2C6F3F.jpg')
        self.browser.switch_to.frame(iframe_add)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(2)
        self.upload_test('//*[@id="layui-layer-iframe3"]', 'D:\\HuaweiMoveData\\Users\\weidajin\\Documents\\project\\image\\F62F47789AC1A7ED4F938612ED2C6F3F.jpg')
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[2]').click()
        sleep(2)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[2]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[4]/input').send_keys('1')
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[5]/input').send_keys('1')
        sleep(1)
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[4]').click()
        sleep(2) 
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[4]/div[6]/div/div[1]/span' ).click()
        self.browser.switch_to.parent_frame()
        sleep(2)
        self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
        sleep(2)
        self.browser.switch_to.default_content()
        sleep(1)
        
        
        
        
        
    def upload_test(self, xpath_path, img_path):
        self.browser.switch_to.parent_frame()
        iframe_upload = self.browser.find_element(By.XPATH, xpath_path)
        self.browser.switch_to.frame(iframe_upload)
        self.browser.find_element(By.XPATH, '/html/body/div/div[2]/div[1]/div[1]/input').send_keys(img_path)
        sleep(4)
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[1]/div[2]/div/ul/li[1]/div/img').click()
        sleep(1)
        self.browser.find_element(By.XPATH, '//*[@id="used"]').click()
        self.browser.switch_to.parent_frame()
        
        
        
        
    def shop_online_test(self):
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[2]/div/ul/li[1]/a').click()
        sleep(1)
        iframe1 = self.browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/ul/li[2]').click()
        sleep(2)
        shop_list = self.browser.find_elements(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr')
        if len(shop_list) > 0:
            shop_list[0].find_element(By.XPATH, './td/div/a[2]').click()
            sleep(2)
            self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
            sleep(2) 
        else:
            pass
        self.browser.switch_to.parent_frame()
        
        
        
            
    def logout_test(self):
        self.browser.switch_to.default_content()
        dt = self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[1]/ul[2]/li[3]/a')
        ActionChains(self.browser).move_to_element(dt).perform()
        sleep(1)
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
        sleep(1)
        
        
    def shop_edit_test(self):
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[2]/div/ul/li[1]/a').click()
        sleep(1)
        iframe1 = self.browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        shop_list = self.browser.find_elements(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr')
        if len(shop_list) > 0:
            shop_list[0].find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[1]').click()
            self.browser.switch_to.frame('layui-layer-iframe1')
            self.browser.find_element(By.NAME, 'name').clear()
            sleep(1)
            self.browser.find_element(By.NAME, 'name').send_keys('编辑商品')
            self.browser.switch_to.parent_frame()
            self.browser.find_element(By.XPATH, '/html/body/div[3]/div[3]/a[1]').click()
            sleep(2)
        self.browser.switch_to.default_content()
        sleep(1)

      
        
if __name__ == '__main__':
    lk = LikeShopTest()
    lk.login_test()
    lk.shop_list_test()
    lk.shop_add_test()
    lk.shop_online_test()
    lk.shop_edit_test()
    print('编辑完成')
    lk.logout_test()